from service.uuyp import request_uuyp_old_detail, request_uuyp_new_detail
from constants.dopple import DOPPLE_PARAMS
from service.csfloat import request_csfloat_list
from service.utils import random_pause, get_usd_to_cny_rate
from rich.progress import Progress
from service.utils import logger


# 整合数据
def integrate_old_dopple():
    result = []
    exchange_rate = get_usd_to_cny_rate()

    progress = Progress()
    progress.start()
    task = progress.add_task("处理多普勒饰品...", total=len(DOPPLE_PARAMS))

    def get_csfloat_and_uu(params={}):
        res_uu = request_uuyp_old_detail(params)
        res_csfloat = request_csfloat_list(params)
        item = {
            "饰品名": f"{params['item_name']} ({params['keyWords']})",
            "YOUPIN价格": None,
            "csfloat价格": None,
            "非大会员利润": None,
            "非大会员利润率": None,
            "大会员利润": None,
            "大会员利润率": None,
        }
        if len(res_csfloat.get("data", [])) > 0:
            data_csfloat = res_csfloat["data"][0]
            item["csfloat价格"] = data_csfloat["price"] / 100 * exchange_rate
        if len(res_uu.get("Data", {}).get("CommodityList", [])) > 0:
            data_uu = res_uu["Data"]["CommodityList"][0]
            item["YOUPIN价格"] = float(data_uu["Price"])
        if item["csfloat价格"] is not None and item["YOUPIN价格"] is not None:
            cost = item["csfloat价格"] * 1.028 * 0.99  # 成本
            no_vip_profit = item["YOUPIN价格"] * 0.98 - cost
            no_vip_profit_rate = (no_vip_profit / cost) if cost > 0 else None
            vip_profit = item["YOUPIN价格"] - cost
            vip_profit_rate = (vip_profit / cost) if cost > 0 else None
            item["非大会员利润"] = no_vip_profit
            item["非大会员利润率"] = no_vip_profit_rate
            item["大会员利润"] = vip_profit
            item["大会员利润率"] = vip_profit_rate
        logger.info(f"{item['饰品名']} 查询成功")
        result.append(item)
        progress.update(task, advance=1)
        random_pause()

    for param in DOPPLE_PARAMS:
        get_csfloat_and_uu(param)

    logger.info("多普勒饰品查询成功")
    progress.stop()

    return result


# 整合新版数据（最好使用国内IP，国外IP容易被拒绝）
# def integrate_new_dopple():
#     result = []
#     exchange_rate = get_usd_to_cny_rate()

#     progress = Progress()
#     progress.start()
#     task = progress.add_task("处理多普勒饰品...", total=len(DOPPLE_PARAMS))

#     def get_csfloat_and_uu(params={}):
#         # 新版本
#         res_uu = request_uuyp_new_detail(params)
#         res_csfloat = request_csfloat_list(params)
#         if len(res_csfloat.get("data", [])) > 0 and len(res_uu.get("Data", [])) > 0:
#             data_csfloat = res_csfloat["data"][0]
#             data_uu = res_uu["Data"][0]
#             csfloat_price = data_csfloat["price"] / 100 * exchange_rate
#             uu_price = float(data_uu["price"])
#             profit_rate = (uu_price - csfloat_price * 1.028) / (csfloat_price * 1.028)
#             result.append(
#                 {
#                     "饰品名": f"{params['item_name']} ({params['keyWords']})",
#                     "YOUPIN价格": uu_price,
#                     "csfloat价格": csfloat_price,
#                     "利润率": profit_rate,
#                 }
#             )
#         progress.update(task, advance=1)
#         random_pause()

#     for param in DOPPLE_PARAMS:
#         get_csfloat_and_uu(param)

#     logger.info("多普勒饰品查询成功")
#     progress.stop()

#     return result
